package com.yby6.system.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.yby6.common.core.domain.SysDictData;
import com.yby6.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yby6.common.utils.DictUtils;
import com.yby6.common.utils.StringUtils;
import com.yby6.system.mapper.SysDictDataMapper;
import com.yby6.system.mapper.SysDictTypeMapper;
import com.yby6.common.core.domain.SysDictType;
import com.yby6.system.service.ISysDictTypeService;

import javax.annotation.PostConstruct;

/**
 * 字典类型Service业务层处理
 *
 * @author yangbuyi
 * @date 2021-06-06
 */
@Service
public class SysDictTypeServiceImpl implements ISysDictTypeService {
    @Autowired
    private SysDictTypeMapper sysDictTypeMapper;

    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    /**
     * 查询字典类型
     *
     * @param dictId 字典类型ID
     * @return 字典类型
     */
    @Override
    public SysDictType selectSysDictTypeById (Long dictId) {
        return sysDictTypeMapper.selectSysDictTypeById(dictId);
    }

    /**
     * 查询字典类型列表
     *
     * @param sysDictType 字典类型
     * @return 字典类型
     */
    @Override
    public List<SysDictType> selectSysDictTypeList (SysDictType sysDictType) {
        return sysDictTypeMapper.selectSysDictTypeList(sysDictType);
    }

    /**
     * 新增字典类型
     *
     * @param sysDictType 字典类型
     * @return 结果
     */
    @Override
    public int insertSysDictType (SysDictType sysDictType) {
        sysDictType.setCreateTime(DateUtils.getNowDate());
        return sysDictTypeMapper.insertSysDictType(sysDictType);
    }

    /**
     * 修改字典类型
     *
     * @param sysDictType 字典类型
     * @return 结果
     */
    @Override
    public int updateSysDictType (SysDictType sysDictType) {
        sysDictType.setUpdateTime(DateUtils.getNowDate());
        return sysDictTypeMapper.updateSysDictType(sysDictType);
    }

    /**
     * 批量删除字典类型
     *
     * @param dictIds 需要删除的字典类型ID
     * @return 结果
     */
    @Override
    public int deleteSysDictTypeByIds (Long[] dictIds) {
        return sysDictTypeMapper.deleteSysDictTypeByIds(dictIds);
    }

    /**
     * 删除字典类型信息
     *
     * @param dictId 字典类型ID
     * @return 结果
     */
    @Override
    public int deleteSysDictTypeById (Long dictId) {
        return sysDictTypeMapper.deleteSysDictTypeById(dictId);
    }

    /**
     * 根据字典类型查询字典数据
     *
     * @param dictType 字典类型
     * @return 字典数据集合信息
     */
    @Override
    public List<SysDictData> selectDictDataByType (String dictType) {
        List<SysDictData> dictDatas = DictUtils.getDictCache(dictType);
        if (StringUtils.isNotEmpty(dictDatas)) {
            return dictDatas;
        }
        dictDatas = sysDictDataMapper.selectDictDataByType(dictType);
        if (StringUtils.isNotEmpty(dictDatas)) {
            DictUtils.setDictCache(dictType, dictDatas);
            return dictDatas;
        }
        return null;
    }

    /**
     * 项目启动时，初始化字典到缓存
     */
    @PostConstruct
    public void init () {
        initSynchronizedDict();
    }


    /**
     * 手动同步
     */
    @Override
    public boolean synchronizedDict () {
        initSynchronizedDict();
        return true;
    }

    private void initSynchronizedDict () {
        List<SysDictType> dictTypeList = sysDictTypeMapper.selectDictTypeAll();
        SysDictData where = new SysDictData();
        where.setStatus("0");
        List<SysDictData> sysDictData = sysDictDataMapper.selectSysDictDataList(where);
        for (SysDictType dictType : dictTypeList) {
            List<SysDictData> dictDatas = sysDictData.stream().filter(item -> item.getDictType().equals(dictType.getDictType())).collect(Collectors.toList());
            DictUtils.setDictCache(dictType.getDictType(), dictDatas);
        }
    }
}
